package testdb;

import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DerbydbTest extends DbTest {

    static final String CONN_STRING = "jdbc:derby:..\\derbydb";

    public DerbydbTest(PrintWriter writer, Record record) {
        super(writer, record);
    }

    @Override
    protected String getName() {
        return "DerbyDB";
    }

    @Override
    protected void insertTest() throws Exception {
        String sql = String.format("INSERT INTO APP.TEST_TABLE (COL1, COL2, COL3, COL4, COL5)"
                + " VALUES ('%s', '%s', '%s', '%s', '%s')",
                record.getCol1(), record.getCol2(), record.getCol3(),
                record.getCol4(), record.getCol5());

        int count = record.getCount();
        for (int j = 0; j < count; j++) {
            connection.createStatement().execute(sql);
        }
    }

    @Override
    protected void selectTest() throws Exception {
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("SELECT * FROM APP.TEST_TABLE");
    }

    @Override
    protected String connect() throws Exception {
        connection = DriverManager.getConnection(CONN_STRING, "APP", "APP");
        return CONN_STRING;
    }

    @Override
    protected void deleteAllData() throws Exception {
        String sql = "delete from APP.TEST_TABLE";
        connection.createStatement().execute(sql);
    }

    @Override
    protected String getConnectionString() {
        return CONN_STRING;
    }
}
